package com.koron.css2.meterService.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BaseBookMapper;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.WaterTypeMapper;
import com.koron.css2.baseConfig.queryBean.MeterFactoryQueryBean;
import com.koron.css2.baseConfig.queryBean.WaterTypeQueryBean;
import com.koron.css2.baseConfig.vo.MeterFactoryVO;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.baseConfig.vo.WaterTypeVO;
import com.koron.css2.meterService.bean.query.MsChgPlanDetailQueryBean;
import com.koron.css2.meterService.dto.MsChgPlanDetailDto;
import com.koron.css2.meterService.mapper.MsChgPlanDetailMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.serviceManage.vo.DictionaryVO;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 换表历史查询
 * @author 周利斌
 *
 */
public class MsChgHistory implements ServerInterface{

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
		try {
			MsChgPlanDetailQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsChgPlanDetailQueryBean.class);
			MsChgPlanDetailMapper mapper = factory.getMapper(MsChgPlanDetailMapper.class);	
			WaterTypeMapper waterTypeMapper = factory.getMapper(WaterTypeMapper.class);	
			if(StringUtils.isNotEmpty(bean.getMeterReadingStaffAccount())) {
				BaseBookMapper baseBookMapper = factory.getMapper(BaseBookMapper.class);	
				List<String> bookNoList = baseBookMapper.selectBookNo(bean.getMeterReadingStaffAccount());
				bean.setBookNos(bookNoList);
			}
			MeterFactoryMapper meterFactoryMapper = factory.getMapper(MeterFactoryMapper.class);
			List<MeterFactoryVO> meterFactorys = meterFactoryMapper.selectMeterFactoryList(new MeterFactoryQueryBean());
			List<WaterTypeVO> waterTypeList = waterTypeMapper.findWaterType(new WaterTypeQueryBean());
			List<DictionaryVO> dvo = Tools.listDicByCode(factory, "OMD");
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			List<MsChgPlanDetailDto> list = mapper.selectHistoryList(bean);
			BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
			List<SelectVO> businessList =  businessAreaMapper.select("",userInfo.getCurWaterCode());
			for(MsChgPlanDetailDto detail : list) {
				// 获取营业区域
				if (!StringUtils.isEmpty(detail.getBusinessArea())) {
					detail.setBusinessArea(businessList.stream().filter(t -> t.getId().equals(detail.getBusinessArea())).findFirst().orElse(new SelectVO()).getName());
				} 
				if (StringUtils.isNotBlank(detail.getMeterFactory())) {
					// 水表厂家
					Optional<MeterFactoryVO> optional = meterFactorys.stream().filter(o -> Objects.equals(o.getId(), detail.getMeterFactory())).findFirst();
					detail.setMeterFactory(optional.orElse(new MeterFactoryVO(detail.getMeterFactory())).getFactoryName());
				}
				if (StringUtils.isNotBlank(detail.getUseWaterType())) {
					// 用水类型
					Optional<WaterTypeVO> optional = waterTypeList.stream().filter(o -> Objects.equals(o.getId(), detail.getUseWaterType())).findFirst();
					detail.setUseWaterType(optional.get().getWaterTypeName());
				}
				/*if(StringUtils.isNotEmpty(detail.getNewMeterBore())) {
					detail.setNewMeterBore(boreMap.get(detail.getNewMeterBore()));
				}
				if(StringUtils.isNotEmpty(detail.getOldMeterBore())) {
					detail.setOldMeterBore(boreMap.get(detail.getOldMeterBore()));
				}*/
				if(detail.getOldHandleWay() != null) {
					for(DictionaryVO vo: dvo) {
						if(vo.getValue().equals(detail.getOldHandleWay())) {
							detail.setOldHandleWayName(vo.getName());
							break;
						}
					}
				}				
			}
			info.setData(new PageInfo<MsChgPlanDetailDto>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("查询失败");
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
